package com.ckm.config;

import com.ckm.creator.DefaultMongoSourceCreator;
import com.ckm.creator.MongoSourceCreator;
import com.ckm.properties.DynamicMongoProperties;
import com.ckm.provider.DynamicMongoSourceProvider;
import com.ckm.provider.YmlDynamicMongoSourceProvider;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

@Configuration
@RequiredArgsConstructor
public class DynamicMongoProviderConfig {

    private final DynamicMongoProperties properties;

    @Bean
    public DynamicMongoSourceProvider ymlDynamicMongoSourceProvider(DefaultMongoSourceCreator defaultMongoSourceCreator){
        return new YmlDynamicMongoSourceProvider(defaultMongoSourceCreator,properties.getDynamic());
    }

    @Bean
    @ConditionalOnMissingBean
    public DefaultMongoSourceCreator defaultMongoSourceCreator(List<MongoSourceCreator> creators){
        DefaultMongoSourceCreator defaultMongoSourceCreator = new DefaultMongoSourceCreator(creators);
        return defaultMongoSourceCreator;
    }
}
